package cn.sll.org.service.impl;

import cn.sll.base.util.BaiduAuditUtils;
import cn.sll.base.util.BussinessException;
import cn.sll.org.domain.Employee;
import cn.sll.org.domain.Shop;
import cn.sll.org.domain.ShopAuditLog;
import cn.sll.org.mapper.EmployeeMapper;
import cn.sll.org.mapper.ShopAuditLogMapper;
import cn.sll.org.mapper.ShopMapper;
import cn.sll.org.service.IShopService;
import cn.sll.base.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sll
 * @since 2022-12-26
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper  shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    // 注入邮件对象
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {

        // 校验数据
        verify(shop);

        // 保存数据
        save(shop);

    }

    @Override
    public void auditPass(ShopAuditLog shopAuditLog){
        // 1 参数校验
        // 2 审核通过修改状态
        // 店铺状态：0待审核，1待激活，2驳回，3已激活
        Shop shop = new Shop();
        shop.setState(1);
        shop.setId(shopAuditLog.getShopId());
        super.update(shop);
        // 2 保存日志
        ShopAuditLog log = new ShopAuditLog();
        // 没有审核员，写死1个
        log.setAuditId(1L);
        log.setAuditTime(new Date());
        log.setNote(shopAuditLog.getNote());
        log.setShopId(shopAuditLog.getShopId());
        shopAuditLogMapper.add(log);

        // 3 发送邮件
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1255535829@qq.com");
            helper.setSubject("测试复杂邮件");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>尊敬的vip，恭喜您入住成功，请及时点击下方链接，激活店铺：</h1> " +
                    "http://localhost:8080/shop/activity/"+shopAuditLog.getShopId()+"'>点击激活</a>",true);
            // 4.添加附件
    //      helper.addAttachment("好东西.jpg",new File("C:\\Users\\Administrator\\Desktop\\22.png"));
            // 5.设置收件人
            helper.setTo("1255535829@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) {
        // 1 参数校验
        // 2 审核通过修改状态
        // 店铺状态：0待审核，1待激活，2驳回，3已激活
        Shop shop = new Shop();
        shop.setState(2);
        shop.setId(shopAuditLog.getShopId());
        super.update(shop);
        // 2 保存日志
        ShopAuditLog log = new ShopAuditLog();
        // 没有审核员，写死1个
        log.setAuditId(1L);
        log.setAuditTime(new Date());
        log.setNote(shopAuditLog.getNote());
        log.setShopId(shopAuditLog.getShopId());
        shopAuditLogMapper.add(log);

        // 3 发送邮件
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1255535829@qq.com");
            helper.setSubject("测试复杂邮件");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>入驻失败，请及时点击下方链接，重新注册店铺：</h1> " +
                    "http://localhost:8081/#/registerAgain?shopId="+shopAuditLog.getShopId()+"'>点击入驻</a>",true);
            // 4.添加附件
            //      helper.addAttachment("好东西.jpg",new File("C:\\Users\\Administrator\\Desktop\\22.png"));
            // 5.设置收件人
            helper.setTo("1255535829@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void settlementAgain(Shop shop) {
        // 百度ai
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BussinessException("店铺名称不合规");
        }
        if (!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            throw new BussinessException("店铺log不合规");
        }
        shop.setState(0);
        super.update(shop);
    }



    @Override
    public void activity(Long id) {
        Shop shop = new Shop();
        shop.setState(3);
        shop.setId(id);
        super.update(shop);
    }

    @Override
    public void saveAll(List<Shop> shops) {
        shops.forEach(System.out::println);
        for (Shop shop:shops) {
            System.out.println(shop);
            shopMapper.add(shop);
        }
    }


    private void save(Shop shop) {
        // 添加店铺
        shop.setState(0);
        shop.setRegisterTime(new Date());
        super.add(shop);

        // 添加员工
        Employee employee = new Employee();
        Employee admin = shop.getAdmin();
        BeanUtils.copyProperties(admin, employee);
        employee.setAge(18);
        employee.setState(1);
        employee.setShopId(shop.getId());
        employeeMapper.add(employee);

        // 修改店铺
        Shop s = new Shop();
        s.setAdminId(employee.getId());
        s.setId(shop.getId());
        super.update(s);
    }

    private void verify(Shop shop) {
        // 校验参数是否为空

        // 百度ai
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BussinessException("店铺名称不合规");
        }
        if (!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            throw new BussinessException("店铺log不合规");
        }

        // 秘密与确认密码是否相等
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new BussinessException("密码不正确");
        }

        // 判断店铺是否存在
        String shopName = shop.getName();
        Shop s = shopMapper.findByShopName(shopName);
        if(s!= null){
            throw new BussinessException("店铺已存在，请登录/更换店铺名。");
        }

        // 判断用户是否管理店铺
        // 根据管理员id查询是否是店铺人员
        String username = shop.getAdmin().getUsername();
        Employee employee = employeeMapper.findByUsername(username);
        // employee不为空 店铺id不为空 证明改用户是店铺人员
        if(employee!=null && employee.getShopId()!=null){
            s = shopMapper.findByAdminId(employee.getId());
            //通过employee的AdminId查询店铺，店铺不为空 证明该用户是店主
            if(s!=null){
                throw new BussinessException("该用户已管理店铺，请更换用户注册");
            }
        }
    }
}
